import io.vertx.core.http.HttpMethod
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.client.WebClient
import io.vertx.kotlin.coroutines.await

class IpHelper(private val client: WebClient) {
  suspend fun getWanIp(): String {
    try {
      val resp = client.request(HttpMethod.GET, 443, "www.ip.cn", "/api/index?ip=&type=0")
        .ssl(true)
        .timeout(5000)
        .send().await()
      if (resp.statusCode() == 200) {
        return JsonObject(resp.body()).getString("ip")
      } else {
        throw ExceptionGetWanIpFailed()
      }
    } catch (e:Exception){
      throw ExceptionGetWanIpFailed()
    }
  }
}
